var http = require('http')

var cheerio = require('cheerio')
var query = 'qq.com|baidu.com|easesale.com|youku.com'
getIp(query)

function getIp(query) {
    let arr = query.split('|').filter(function(e) {
        return e != ''
    })

    for (let k of arr) {
        getIpInner(k)
    }

    function getIpInner(q) {
        q = encodeURI(q)
        var buf = Buffer.from('', 'utf-8');
        var userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0'
        var req = http.request({
            method: 'GET',
            host: 'ip.cn',
            path: `/index.php?ip=${q}`
        }, res => {
            // console.log(res.statusCode)
            // console.log(res.headers)

            res.on('data', function(d) {
                $ = cheerio.load(d.toString())
                let plist = $('#result>.well>p')
                if (plist.length > 0) {
                    console.log(q, plist.eq(0).text().replace('您查询的 IP：', ''), plist.eq(1).text().replace('所在地理位置：', ''))
                } else {
                    console.log($('#result>.well').text())
                }
            })
        });
        req.setHeader('User-Agent', userAgent)

        req.write(buf);
        req.end();
    }
}